package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Recruitment;
import com.ruoyi.system.domain.RecruitmentResume;
import com.ruoyi.system.model.HiringRecord;
import com.ruoyi.system.service.IRecruitmentResumeService;
import com.ruoyi.system.service.IRecruitmentService;
import com.ruoyi.system.service.IResumeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 招聘记录管理Controller
 *
 * @author ruoyi
 * @date 2022-11-05
 */
@Api("招聘记录管理")
@Controller
@RequestMapping("/system/hiringRecord")
public class HiringRecordController extends BaseController {
    private String prefix = "system/hiringRecord";

    @Resource
    private IRecruitmentResumeService recruitmentResumeService;

    @Resource
    private IRecruitmentService recruitmentService;

    @RequiresPermissions("system:hiringRecord:view")
    @ApiOperation("跳转招聘记录管理列表页")
    @GetMapping()
    public String resume()
    {
        return prefix + "/hiringRecord";
    }

    /**
     * 查询招聘记录管理列表
     * @param recruitmentResume 招聘管理记录对象
     * @return
     */
    @ApiOperation("查询招聘记录管理列表")
    @RequiresPermissions("system:hiringRecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecruitmentResume recruitmentResume)
    {
        String userName = ShiroUtils.getSysUser().getUserName();
        //招聘者发送面试邀请的简历
        recruitmentResume.setFlag(1);
        //返回结果
        List<HiringRecord> hiringRecords = new ArrayList<>();
        startPage();
        List<RecruitmentResume> list = recruitmentResumeService.selectRecruitmentResumeList(recruitmentResume);
        //用一个map记录每条招聘信息的发送面试邀请数
        HashMap<Long, Integer> map = new HashMap<Long, Integer>();
        List<Long> resRecIds = new ArrayList<>();
        for (RecruitmentResume resume : list) {
            Recruitment recruitment = recruitmentService.selectRecruitmentByRecruitmentId(resume.getRecruitmentId());
            if (ObjectUtils.isNotEmpty(recruitment.getRecruitmentCompany()) && !recruitment.getRecruitmentCompany().equals(userName)) {
                continue;
            }
            Set<Long> set = map.keySet();
            if (set.size() > 0 && set.contains(resume.getRecruitmentId())) {
                Integer integer = map.get(resume.getRecruitmentId());
                map.remove(map.remove(resume.getRecruitmentId()));
                map.put(resume.getRecruitmentId(), integer + 1);
            } else {
                map.put(resume.getRecruitmentId(), 1);
                resRecIds.add(resume.getRecResId());
            }
        }
        for (Map.Entry<Long, Integer> entry : map.entrySet()) {
            int index = 0;
            Long recruitmentId = entry.getKey();
            Integer recruitmentNum = entry.getValue();
            RecruitmentResume recRes = new RecruitmentResume();
            recRes.setRecruitmentId(recruitmentId);
            recRes.setFlag(0);
            int resumeNum = recruitmentResumeService.selectRecruitmentResumeList(recRes).size();
            HiringRecord hiringRecord = new HiringRecord();
            hiringRecord.setRecResId(resRecIds.get(index++));
            hiringRecord.setRecruitmentId(recruitmentId);
            hiringRecord.setRecruitmentName(recruitmentService.selectRecruitmentByRecruitmentId(recruitmentId).getRecruitmentName());
            hiringRecord.setNum(recruitmentNum);
            hiringRecord.setResumeNum(resumeNum);
            hiringRecords.add(hiringRecord);
        }
        return getDataTable(hiringRecords);
    }

    /**
     * 导出招聘记录管理列表
     * @param recruitmentResume 招聘管理记录对象
     * @return
     */
    @ApiOperation("导出招聘记录管理列表")
    @RequiresPermissions("system:hiringRecord:export")
    @Log(title = "招聘记录管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecruitmentResume recruitmentResume)
    {
        List<RecruitmentResume> list = recruitmentResumeService.selectRecruitmentResumeList(recruitmentResume);
        ExcelUtil<RecruitmentResume> util = new ExcelUtil<RecruitmentResume>(RecruitmentResume.class);
        return util.exportExcel(list, "招聘记录管理数据");
    }

    /**
     * 删除招聘记录管理
     */
    @ApiOperation("删除招聘记录管理")
    @RequiresPermissions("system:hiringRecord:remove")
    @Log(title = "招聘记录管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(recruitmentResumeService.deleteRecruitmentResumeByRecResIds(ids));
    }
}
